package com.ding.ding

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.provider.Settings
import android.text.TextUtils
import android.util.Log
import com.auto.ding.util.AcessibilityApi


object AppKit {
    /**
     * 检查系统设置：是否开启辅助服务
     *
     * @param service 辅助服务
     */
    fun isSettingOpen(service: Class<*>, cxt: Context): Boolean {
        try {
            val enable = Settings.Secure.getInt(
                cxt.contentResolver,
                Settings.Secure.ACCESSIBILITY_ENABLED,
                0
            )
            if (enable != 1)
                return false
            val services = Settings.Secure.getString(
                cxt.contentResolver,
                Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
            )
            if (!TextUtils.isEmpty(services)) {
                val split = TextUtils.SimpleStringSplitter(':')
                split.setString(services)
                while (split.hasNext()) { // 遍历所有已开启的辅助服务名
                    if (split.next().equals(
                            cxt.packageName + "/" + service.name,
                            ignoreCase = true
                        )
                    )
                        return true
                }
            }
        } catch (e: Throwable) {//若出现异常，则说明该手机设置被厂商篡改了,需要适配
            e.printStackTrace()
        }

        return false
    }

    /**
     * 跳转到系统设置：开启辅助服务
     */
    fun jumpToSetting(cxt: Context) {
        try {
            cxt.startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))
        } catch (e: Throwable) {//若出现异常，则说明该手机设置被厂商篡改了,需要适配
            try {
                val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                cxt.startActivity(intent)
            } catch (e2: Throwable) {
                e.printStackTrace()
            }

        }

    }
    /**
     * 根据包名打开应用
     */
    fun openApplicationByPackage(
        packageName: String = "com.alibaba.android.rimet",
        context: Context
    ) {
        Log.i(" packageName -->", packageName)
        val packageManager = context.packageManager
        var pi: PackageInfo? = null
        try {
            pi = packageManager.getPackageInfo(packageName, 0)
        } catch (e: PackageManager.NameNotFoundException) {
        }
        val resolveIntent = Intent(Intent.ACTION_MAIN, null)
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER)
        resolveIntent.setPackage(pi!!.packageName)
        val apps = packageManager.queryIntentActivities(resolveIntent, 0)
        val resolveInfo = apps.iterator().next()
        if (resolveInfo != null) {
            val className = resolveInfo.activityInfo.name
            val intent = Intent(Intent.ACTION_MAIN)
            intent.addCategory(Intent.CATEGORY_LAUNCHER)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            val cn = ComponentName(packageName, className)
            intent.component = cn
            context.startActivity(intent)
        }
    }

    /**
     * 点击物理按键--返回键
     */
    fun clickBackButton() {
        AcessibilityApi.performAction(AcessibilityApi.ActionType.BACK)
    }


}